<?
/************************************************************************/
/* Andormeda-Web-DeskTop  test preview                                  */
/* ==================================================================== */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/


// previene che il blocco sia eseguito direttamente e redirige a index.php
//if (eregi("desk.php",$_SERVER['PHP_SELF']))
//{ Header("Location: ../../index.php");
//	die();
//}

// restituisce il percorso della security box. se la sb non esiste allora la crea

function sb_get(){
    if (!file_exists("../../datas/sb"))mkdir("../../datas/sb",0755);
    $hdir=opendir("../../datas/sb");
    while (false !== ($f= readdir($hdir)))
      if ($f[0]!='.' && $f!="index.php") $sbdir=$f;
    closedir($hdir);
  if ($sbdir==""){
    $sbdir=rand(1000,9000)."-".rand(1000,9000)."-".rand(1000,9000)."-".rand(1000,9000)."-".rand(1000,9000)."-".rand(1000,9000)."-".rand(1000,9000)."-".rand(1000,9000);
    echo $sbdir;
    mkdir("../../datas/sb/".$sbdir,0755);
    $fp=fopen("../../datas/sb/index.php","a");
    fwrite($fp,"\n");
    fclose($fp);
  }
  return $sbdir;
}

/***************************************************************************/


//verifica che l'utente e' admin
function admin_isadmin(){
    return admin_verifyIP();
}
/***************************************************************************/
// cancella l'ip dell'amministratore
function admin_removeIP(){
  $ip="no-ip";
  admin_writelog("amdimin:remove ip administrator");
  $fp=fopen("../../datas/sb/".sb_get()."/admin_ip","w");
  fwrite($fp,$ip );
  fclose($fp);
}
/***************************************************************************/

// verifica l'ip dell'amministratore
function admin_verifyIP(){
 $ip=$_SERVER["REMOTE_ADDR"];
 if (!file_exists("../../datas/sb/".sb_get()."/admin_ip"))
    admin_removeIP();
 $registered_ip=@join(@file("../../datas/sb/".sb_get()."/admin_ip"));
 if ($registered_ip==$ip){
    //admin_writelog("verifyip:ok");
    return true;
    }
 else{
    //admin_writelog("verifyip:failed");
    return false;
    }
}
/***************************************************************************/

// verifica l'identita dell'utente
function user_getuser(){
      $tmp=explode("||",$_COOKIE['user']);
      $user=$tmp[0];
      $password=$tmp[1];

      if (file_exists("../../datas/sb/".sb_get()."/users/$user") && $user!="")
        include "../../datas/sb/".sb_get()."/users/$user";
      else{
	   if ($user!="")
		echo "<script language=javascript>window.location='index.php?user_op=logout';</script>";
        return "";
    }

      if ($user_password==$password)
        return $user;
      else
        return "";
}

//************************************************************************/

// restituisce il valore di una chiave di un utente
function user_getkey($user,$key){
    if ($user!="" && file_exists("datas/sb/".sb_get()."/users/$user")){
	include "../../datas/sb/".sb_get()."/users/$user";
	return ${$key};
    }
    else{
	return "";
    }
}

/************************************************************************/

//crea un gruppo
function group_create($group){
    if (!file_exists("../../datas/sb/".sb_get()."/group")){
	mkdir("../../datas/sb/".sb_get()."/group",0755);
	mkdir("../../datas/sb/".sb_get()."/group/groups",0755);
	mkdir("../../datas/sb/".sb_get()."/group/users",0755);
    }
    if (!file_exists("../../datas/sb/".sb_get()."/group/groups/$group")){
	$fp=fopen("../../datas/sb/".sb_get()."/group/groups/$group","w");
	fclose($fp);
    }
}

/************************************************************************/

//verifica se quel user e' del gruppo

function group_getusergroup($group,$user){
    group_create($group);
    if (file_exists("../../datas/sb/".sb_get()."/group//groups/$group")){
	foreach(@file("../../datas/sb/".sb_get()."/group/groups/$group") as $l){
	    if ($l==$user."\n")
		return 1;
	}
    }
    return 0;
}
/************************************************************************/

//la rutine che salva la configurazione generale: il nuovo mondo!!!!
function get_config($var,$default){
    if (!file_exists("../../datas/config/$var")){
	$f=fopen("../../datas/config/$var","w");fwrite($f,$default);fclose($f);}
    return @join(@file("../../datas/config/$var"));
}
/************************************************************************/

//restituisce la lingua impostata

function lang(){
  $lang=user_getkey(user_getuser(),"user_lang");
  if ($lang!="")
    return substr($lang,0,2);
  else
    return get_config("lang","it");

}
/************************************************************************/



//--------------------------------------------------------------------------

// if (admin_isadmin()){

if ((group_getusergroup("webdesktop",user_getuser()) or admin_isadmin()) ){ /* utente desktop */ ?>

<HTML> 
  <HEAD>
	 <TITLE>Demo x-Desktop</TITLE>
  </HEAD> <FRAMESET ROWS="100%"><FRAME SRC="x-desktop.html" noresize="noresize" scrolling="no"><NOFRAMES>
  <BODY>
	 <P><I>Demo xDesktop</I></P>
	 </BODY> </NOFRAMES></FRAMESET>
</HTML>

<? }
//   }
?>

